package com.chenxi.vhr.controller.emp;

import com.chenxi.vhr.bean.VhrEmployee;
import com.chenxi.vhr.bean.common.NasDeleteListForm;
import com.chenxi.vhr.bean.common.NasExcelForm;
import com.chenxi.vhr.bean.common.Result;
import com.chenxi.vhr.bean.common.ResultPageInfo;
import com.chenxi.vhr.bean.vo.EmployeeVo;
import com.chenxi.vhr.service.VhrEmployeeService;
import com.chenxi.vhr.utils.ExcelUtil;
import com.chenxi.vhr.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 员工管理
 * @author : zhangchenxi
 * @create : 2022-02-10
 **/
@RestController
@RequestMapping("/employee/basic")
public class EmpController {

    @Autowired
    VhrEmployeeService employeeService;

    @GetMapping("/{page}/{size}")
    public ResultPageInfo listPathPage(@PathVariable Integer page, @PathVariable Integer size, EmployeeVo employee) {
        ResultPageInfo resultPageInfo = employeeService.listPage(page, size, employee);
        return resultPageInfo;
    }

    @PostMapping
    public Result add(@RequestBody VhrEmployee employee) {
        return employeeService.saveEmployee(employee);
    }

    @PutMapping
    public Result update(@RequestBody VhrEmployee employee) {
        return employeeService.updateEmployee(employee);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        return employeeService.removeById(id) ? Result.ok("删除成功！"): Result.error("删除失败！");
    }

    @DeleteMapping("/batch")
    public Result batchDelete(@RequestBody NasDeleteListForm form) {
        return employeeService.removeByIds(form.getIdList()) ? Result.ok("批量删除成功！"): Result.error("批量删除失败！");
    }

    /**
     * 生成最新可用的工号
     * @return
     */
    @GetMapping("/genWorkNo")
    public Result genWorkNo() {
        return employeeService.genWorkNo();
    }


    /**
     * 文件导出
     * @param response
     * @param form
     * @throws Exception
     */
    @PostMapping("/export")
    public void exportExcel(HttpServletResponse response, @RequestBody(required = false) NasExcelForm form) throws Exception {
        ResultPageInfo resultPageInfo = employeeService.listPage(null, null, null);
        List<EmployeeVo> list = (List<EmployeeVo>) resultPageInfo.getObj();
        ExcelUtil<EmployeeVo> excelUtil = new ExcelUtil<>(EmployeeVo.class);
        excelUtil.exportExcel(response, form, list);
    }

    @PostMapping("/import")
    public Result importExcel(MultipartFile file, @RequestParam List<String> fieldList) {
        Result result = Result.ok();
        try {
            result = employeeService.importExcel(file, fieldList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData() {
        List<EmployeeVo> list = (List<EmployeeVo>) employeeService.listPage(null, null, null).getObj();
        return POIUtils.employee2Excel(list);
    }

}
